#include <iostream>
#include <vector>`
#include <set>
#include <algorithm>

using std::vector;
using std::cin;
using std::cout;
using std::set;
using std::pair;
using std::make_pair;
using std::min;

class Field {
	struct Point {
		int x, y, weight;
		Point(): x(0), y(0), weight(0){}
		Point(int new_x, int new_y, int new_weight):
			x(new_x), y(new_y), weight(new_weight) {}
		bool operator<(Point other) const {
			return weight < other.weight;
		}
	};

	int height;
	int width;
	vector < vector <Point> > field;

	void make_neighbour_list(Point &point, vector <Point> &list) {
		if (point.x < height - 1) {
			list.push_back(field[point.x + 1][point.y]);
		}
		if (point.y < width - 1) {
			list.push_back(field[point.x][point.y + 1]);
		}
	}

 public:
	Field(int new_height, int new_weight) {
		height = new_height;
		width = new_weight;
		vector <Point> line(width);
		field.assign(height, line);
		for (int line = 0; line != height; ++line) {
			for (int col = 0; col != width; ++col) {
				int weight;
				cin >> weight;
				field[line][col] = Point(line, col, weight);
			}
		}
	}

	vector<Point> operator[](int &line) {
		return field[line];
	}

	int find_path_width() {
		Point begin = field[0][0];
		vector < vector <int> > path_weight;
		vector <int> line(width, 1000000001);
		path_weight.assign(height, line);
		path_weight[0][0] = begin.weight;

		for (int i = 0; i < height; ++i) {
			for (int j = 0; j < width; ++j) {
				if (!(i == 0 && j == 0)) {
					int up = 1000000001;
					int left = 1000000001;
					if (i > 0) {
						up = path_weight[i - 1][j];
					}
					if (j > 0) {
						left = path_weight[i][j - 1];
					}
					path_weight[i][j] = field[i][j].weight + min(up, left);
				}
			}
		}
		return path_weight[height - 1][width - 1];
	}
};

int main () {
	int n, m;
	cin >> n >> m;
	Field field(n, m);
	cout << field.find_path_width();
}